<?php

namespace App\Http\Controllers\Api;

use App\GroupBuyItem;
use App\GroupMaster;
use App\Http\Controllers\ApiResponse;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class GroupMasterController extends Controller
{
    public $uid;
    use ApiResponse;
    public function __construct()
    {
        $this->uid=auth('api')->id();
    }

    public function group_buy(Request $request)
    {
        $list=GroupBuyItem::with('group_buy')->where('uid',$this->uid)->latest()->paginate($request->get('size',10));
        return $this->data($list);
    }
    public function show($id)
    {
        $result=GroupMaster::with(["group_items"=>function($query){
            $query->with('user')->where('status',1);
        },'goods'])->where('id',$id)->first();
        return $this->data($result);
    }
    public function recent_group_buy(Request $request)
    {
        $result=GroupBuyItem::with(['group_master'])->latest()->where('uid',$this->uid)->where('status','>=',0)->first();
        return $this->data($result);
    }
    public function user_group($id)
    {
        $result=GroupBuyItem::with(["group_master"=>function($query){
            return $query->with('goods');
        }])->where('id',$id)->first();
        return $this->data($result);
    }
}
